package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import model.userhandler.*;
import model.information.*;

/**
 * LifeManager 1.0<br>
 * Institut for Informations- og Medievidenskab<br>
 * Aarhus Universitet.
 *
 * @author Tobias Møller Kjærsgaard
 * @author Hjalte Blichfeldt Flyger
 * @author Gitte Simonsen
 * @author Claus Lund
 */
public class Main {

    /**
     * Look for arguments and execute the corresponding code.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Look for arguments:
        if(args.length == 0) {
            //Use the Mac OS X screen menubar, and set a menubar-name for the application:
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "LifeManager");
            //Import the navtive look-and-feel of the operating system:
            try {
                javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
                //ex.printStackTrace();
            }
            //Create the controller and let it handle the rest.
            new Controller();
        } else if(args[0].equals("--create")) {
            createAndSaveUser();
        } else if(args[0].equals("--open")) {
            showInformation(openUser());
        }
    }


    public static User openUser() {
        FileInputStream fIn = null;
        ObjectInputStream oIn = null;
        User usr = null;

        try {
            File file = new File("user.ser");
            fIn = new FileInputStream(file);
            oIn = new ObjectInputStream(fIn);
            usr = (User)oIn.readObject();
            oIn.close();
            fIn.close();
            System.out.println("Loaded User from file: " + file.getAbsolutePath());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return usr;
    }

    public static void createAndSaveUser() {
        User user1 = new User("testuser", "testpasswordd", "Test", "Testesen", new InformationManager());
        InformationManager iManager = user1.getIManager();

        //Contacts test:
        Contact c1 = new Contact("Karsten", "Selleri");
        Contact c2 = new Contact("John", "Johnson");
        iManager.addContact(c1);
        iManager.addContact(c2);

        //Events test:
        Event ev1 = new Event("Læsegruppemøde", "IT-Huset", "Java er min ven");
        Event ev2 = new Event("Læsegruppemøde", "IT-Huset", "DigiMod eksamen er sjov", 2009, 5, 20, 9, 15, 2009, 05, 20, 16, 00);
        iManager.addEvent(ev1);
        iManager.addEvent(ev2);

        //Journal test:
        JournalEntry je1 = new JournalEntry("Eksamensangst", "Kære dagbog... Jeg tror jeg lider af eksamensangst...");
        JournalEntry je2 = new JournalEntry("Ligemeget", "Kære dagbog, glem hvad jeg skrev i går, jeg var totalt nede...");
        iManager.addJournalEntry(je1);
        iManager.addJournalEntry(je2);

        //Links test:
        Link li1 = new Link("dr.dk", "http://www.dr.dk/");
        Link li2 = new Link("AULA", "https://aula.au.dk/");
        iManager.addLink(li1);
        iManager.addLink(li2);

        //Memos test:
        Memo me1 = new Memo("Mælk, juice, vaskepulver");
        Memo me2 = new Memo("Lav et awesome program til DigiMod eksamen!");
        iManager.addMemo(me1);
        iManager.addMemo(me2);

        //Passwords test:
        Password pw1 = new Password("Netbank", "peter_valentin", "*kaempeOvertraeK$08");
        Password pw2 = new Password("Facebook", "petervalentin@roskildebank.dk", "*jegErEnOndBankmand$93");
        iManager.addPassword(pw1);
        iManager.addPassword(pw2);

        //Researches test:
        Research re1 = new Research("Flyvende tæppe", "Noter til at lave et flyvende tæppe, der skal bruges: 1 stk. egyptisk tæppe og 1 stk. gal araber.");
        Research re2 = new Research("Svineinfluenza", "NB: tag IKKE til Mexico!");
        iManager.addResearch(re1);
        iManager.addResearch(re2);

        FileOutputStream fOut = null;
        ObjectOutputStream oOut = null;

        try {
            File file = new File("user.ser");
            fOut = new FileOutputStream("user.ser");
            oOut = new ObjectOutputStream(fOut);
            oOut.writeObject(user1);
            oOut.close();
            fOut.close();
            System.out.println("Saved user to file: " + file.getAbsolutePath());
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void showInformation(User usr) {
        InformationManager iManager = usr.getIManager();

        System.out.println("\n- Contacts: ----------------");
        for (Contact c: iManager.getContacts()) {
            System.out.println(c.getFirstName() + " " + c.getLastName());
        }

        System.out.println("\n- Events: ----------------");
        for (Event e: iManager.getEvents()) {
            System.out.println(e.toString());
        }

        System.out.println("\n- Journal: ----------------");
        for (JournalEntry j: iManager.getJournal()) {
            System.out.println(j.getDate().getTime().toString() + ": " + j.getTitle() + "\n" + j.getText());
        }

        System.out.println("\n- Links: ----------------");
        for (Link l: iManager.getLinks()) {
            System.out.println(l.getTitle() + ": " + l.getUrl());
        }

        System.out.println("\n- Memos: ----------------");
        for (Memo m: iManager.getMemos()) {
            System.out.println(m.getDate().getTime().toString() + ":\n" + m.getNote());
        }

        System.out.println("\n- Passwords: ----------------");
        for (Password p: iManager.getPasswords()) {
            System.out.println(p.getTitle() + " username and password: " + p.getUsername() + " / " + p.getPassword());
        }

        System.out.println("\n- Researches: ----------------");
        for (Research r: iManager.getResearchnotes()) {
            System.out.println(r.getTitle() + ": \n" + r.getText());
        }
    }
}